package own.stu.inter.crawler;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;

@Slf4j
@AllArgsConstructor
@Data
public class DirInfoTask {

    Crawler crawler;

    public static void main(String[] args) throws IOException {

        final Crawler crawler = new Crawler();
        DirInfoTask task = new DirInfoTask(crawler);
        task.getChapterDirList("https://m.ahfgb.com/25/25741/index.html");
        crawler.shutdown();
    }

    public String getChapterDirList(String path) {
        log.info("get DirInfo {}", path);
        String body = null;
        try {

            do {
                body = crawler.getResponseBody(path);
                String chapterListContent = ContentAnalyze.getDirListBody(body);
                List<String> dirList = ContentAnalyze.getDirList(chapterListContent);

                crawler.addDirList(dirList);

                path = ContentAnalyze.getNextDirList(body);
                if (StringUtils.isBlank(path)) {
                    break;
                }

            } while (true);
        } catch (Exception e) {
            log.error("error get content: {}, error: ", path, e);
        }
        return "";
    }
}
